﻿using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class SysActionAppService : BaseAppService<SysAction, int, SysActionSearch, ISysActionRepository>, ISysActionAppService
    {
        private IUserAccountAppService UserAccountAppService { get; set; }
        private IRoleAppService RoleAppService { get; set; }
        private ISysControllerAppService SysControllerAppService { get; set; }

        public BoolGenericResult<UserAccount> AddActionToUserAccount(int userAccountId, bool onlyQuerySelfData, bool cannotQueryPurchaseCostAndAmount,
            bool cannotQuerySalesCostAndAmount, IList<SysAction> actions)
        {
            Init();
            this.UserAccountAppService = this.SpringContext.GetObject(SystemModuleConst.USERACCOUNTAPPSERVICE) as IUserAccountAppService;
            this.SysControllerAppService = this.SpringContext.GetObject(SystemModuleConst.SYSCONTROLLERAPPSERVICE) as ISysControllerAppService;
            this.RoleAppService = this.SpringContext.GetObject(SystemModuleConst.ROLEAPPSERVICE) as IRoleAppService;
            UserAccount userAccount = this.UserAccountAppService.GetById(userAccountId);

            IList<SysController> controllers = this.SysControllerAppService.GetBySearch(new SysControllerSearch()).ToList();
            List<SysAction> allActions = new List<SysAction>();
            IList<SysAction> permissionActions = new List<SysAction>();
            foreach (SysController controller in controllers)
            {
                allActions.AddRange(controller.Actions);
            }
            foreach (SysAction sa in actions)
            {
                foreach (SysAction action in allActions)
                {
                    if (sa.Id == action.Id)
                    {
                        permissionActions.Add(action);
                    }
                }
            }

            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Administrator admin = new Administrator(employee);

            BoolGenericResult<UserAccount> result = admin.AddActionToUserAccount(userAccount, controllers, permissionActions);
            userAccount.UserAccountProperty.OnlyQuerySelfData = onlyQuerySelfData;
            userAccount.UserAccountProperty.CannotQueryPurchaseCostAndAmount = cannotQueryPurchaseCostAndAmount;
            userAccount.UserAccountProperty.CannotQuerySalesCostAndAmount = cannotQuerySalesCostAndAmount;
            if (result.IsSuccess)
            {
                if (result.Object.Role.Id == 0)
                {
                    this.RoleAppService.Add(result.Object.Role);
                    this.UserAccountAppService.Update(result.Object);
                }
                else
                {
                    this.RoleAppService.Update(result.Object.Role);
                }
            }
            result.Messages.Add("保存成功");

            return result;
        }
    }
}
